from PIL import Image

def longitudinal_merge(img_names, output_img_name):
    img_num = len(img_names)
    img_flag = Image.open(img_names[0])
    width = img_flag.size[0]
    height = img_flag.size[1]

    output_img = Image.new("RGB", (width, height * img_num))
    output_pix = output_img.load()
    for i in range(img_num):
        img = Image.open(img_names[i])
        pix = img.load()

        for j in range(height):
            for k in range(width):
                output_pix[k, i * height + j] = pix[k, j]

    output_img.save(output_img_name)

def transverse_merge(img_names, output_img_name):
    img_num = len(img_names)
    img_flag = Image.open(img_names[0])
    width = img_flag.size[0]
    height = img_flag.size[1]

    output_img = Image.new("RGB", (width * img_num, height))
    output_pix = output_img.load()
    for i in range(img_num):
        img = Image.open(img_names[i])
        pix = img.load()

        for j in range(height):
            for k in range(width):
                output_pix[i * width + k, j] = pix[k, j]

    output_img.save(output_img_name)